<template>
  <div>
    <!-- 搜索区域 -->
    <div style="margin-bottom: 20px;">
      <el-input
        v-model="searchForm.code"
        placeholder="股票代码"
        style="width: 200px; margin-right: 10px;"
        clearable
      ></el-input>
      <el-input
        v-model="searchForm.name"
        placeholder="股票名称"
        style="width: 200px; margin-right: 10px;"
        clearable
      ></el-input>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>

    <!-- 操作按钮 -->
    <div style="margin-bottom: 10px;">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button
        type="danger"
        @click="handleBatchDelete"
        :disabled="multipleSelection.length === 0"
      >
        批量删除
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="code" label="代码" width="120"></el-table-column>
      <el-table-column prop="name" label="名称" width="150"></el-table-column>
      <el-table-column prop="url" label="公司网址" show-overflow-tooltip></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleView(scope.row)" type="text" size="medium">查看</el-button>
          <el-button @click="handleEdit(scope.row)" type="text" size="medium">编辑</el-button>
          <el-button @click="addStockRevenueQuarter(scope.row)" type="text" size="medium">添加营业总收入-单季度</el-button>
          <el-button @click="addStockProfit(scope.row)" type="text" size="medium">添加净利润-单季度</el-button>
          <el-button
            @click="handleDelete(scope.row)"
            type="text"
            size="medium"
            style="color: #f56c6c;"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="margin-top: 20px; text-align: right;" class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Stock",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      loading: false,
      searchForm: {
        code: '',
        name: ''
      },
      currPage: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    // 加载数据
    load() {
      this.loading = true;
      this.request.get("/stock/page", {
        params: {
          currPage: this.currPage,
          size: this.size,
          code: this.searchForm.code,
          name: this.searchForm.name
        }
      }).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.total = res.data.totalCount;
        }
      }).catch(() => {
        this.loading = false;
      });
    },
    // 搜索
    handleSearch() {
      this.size = 10;
      this.load();
    },
    // 重置
    handleReset() {
      this.searchForm = {
        code: '',
        name: ''
      };
      this.size = 10;
      this.load();
    },
    // 新增按钮处理函数
    handleAdd() {
      this.$router.push({
        name: 'StockForm'
      });
    },
    // 查看按钮处理函数
    handleView(row) {
      this.$router.push({
        name: 'StockView',
        query: { id: row.id }
      });
    },
    // 编辑按钮处理函数
    handleEdit(row) {
      this.$router.push({
        name: 'StockForm',
        query: { id: row.id }
      });
    },
    addStockRevenueQuarter(row) {
      this.$router.push({
        name: 'StockRevenueQuarterForm',
        query: { stockId: row.id, code: row.code }
      });
    },
    addStockProfit(row) {
      this.$router.push({
        name: 'StockProfitForm',
        query: { stockId: row.id, code: row.code }
      });
    },
    // 删除单条记录
    handleDelete(row) {
      this.$confirm('确定要删除该股票信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete(`/stock/${row.id}`).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.load();
          }
        });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    // 批量删除
    handleBatchDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请先选择要删除的数据');
        return;
      }
      this.$confirm(`确定要删除选中的 ${this.multipleSelection.length} 条数据吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.multipleSelection.map(item => item.id);
        this.request.post('/stock/del/batch', ids).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.load();
          }
        });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    // 表格选择变化
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页大小变化
    handleSizeChange(val) {
      this.pageSize = val;
      this.size = 10;
      this.load();
    },
    // 页码变化
    handleCurrentChange(val) {
      this.size = val;
      this.load();
    }
  }
}
</script>

<style scoped>
</style>